package ufs.file;

import java.io.EOFException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

import ufs.cmds.UFSCommandsConstants;

public class UfsStream {

	private RandomAccessFile raf;
	private String fileName;
	
	public UfsStream(String fileName) {
		this.fileName = fileName;
		File fs = new File(fileName);
		if(fs.exists()) {
			try {
				raf = new RandomAccessFile(fs,"rw");
			} catch (FileNotFoundException e) {
				
			}
		}
	}
	
	public boolean CreateFS() {
		File fs = new File(fileName);
		if(fs.exists()) {
			System.out.println( UFSCommandsConstants.FS_ALREADY_EXISTS );
			return true;
		}
		try {
			fs.createNewFile();
			raf = new RandomAccessFile(fs,"rw");
		} catch (FileNotFoundException e) {
			System.out.println( UFSCommandsConstants.FILE_NOT_CREATED );
			return false;
		} catch (IOException e) {
			System.out.println( UFSCommandsConstants.FILE_NOT_CREATED );
			System.out.println( e.getMessage() );
			return false;
		}
		return true;
	}
	
	public boolean exists() {
		File fs = new File(fileName);
		if(fs.exists()) {
			return true;
		}
		return false;
	}

		
	/****************************************************************************************************************
	 * 																												*
	 * 													WRITERS														*
	 * 																												*
	 * **************************************************************************************************************/
	
	public void wByte(byte b) {

		try {
			raf.writeByte(b);
		} catch (IOException e) {
			System.out.println( e.getMessage() );
		}
	}
	
	public void wShort(short s) {

		try {
			raf.writeShort(s);
		} catch (IOException e) {
			System.out.println( e.getMessage() );
		}
	}

	public void wInt(int n) {

		try {
			raf.writeInt(n);
		} catch (IOException e) {
			System.out.println( e.getMessage() );
		}
	}

	public void wString(String s, int size) {
	
		String strToWrite = s;
		int padSize = 0;
		if(s.length() < size) {
			padSize = size - s.length();
			StringBuilder pad = new StringBuilder(" ");
			
			for(int i=1; i<padSize; i++) {
				pad.append(" ");
			}
			strToWrite += pad;
			
		}
			
		try {
			raf.writeBytes(strToWrite);
		} catch (IOException e) {
			System.out.println( e.getMessage() );
		}
	}

	/****************************************************************************************************************
	 * 																												*
	 * 													READERS														*
	 * 																												*
	 * **************************************************************************************************************/
	
	public Integer rByte() {
		
		try {
			return raf.read();
		} catch (IOException e) {
			System.out.println( e.getMessage() );
			return null;
		}
	}
	
	public Short rShort() {

		try {
			 return raf.readShort();
		} catch (IOException e) {
			System.out.println( e.getMessage() );
			return null;
		}
	}
	
	public Integer rInt() {
		
		try {
			return raf.readInt();
		} catch (EOFException e) {
			System.out.println( e.getMessage() );
			return null;
		} catch (IOException e) {
			System.out.println( e.getMessage() );
			return null;
		}
	}
	
	public void seek(int n) {
		
		try {
			raf.seek(n);
		} catch (IOException e) {
			System.out.println( e.getMessage() );
		}
	}	
	
	public void close() {
		
		try {
			raf.close();
		} catch (IOException e) {
			System.out.println( e.getMessage() );
		}
	}
}
